package com.teaclipse.presentation;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class TeaFileChooser extends JFileChooser {

	public TeaFileChooser()
	{
		super();
		this.setFileFilter(new TeaFileFilter());
		this.setAcceptAllFileFilterUsed(false);
	}

	public String getChoosenFileName() throws IOException
	{
		return this.getSelectedFile().getCanonicalPath();
	}



	class TeaFileFilter extends FileFilter
	{
		 //Accept all directories and all gif, jpg, tiff, or png files.
	    public boolean accept(File f) {
	        if (f.isDirectory()) {
	            return true;
	        }

	        String extension = getExtension(f);
	        if (extension != null) {
	            if (extension.equals("tea")) {
	                    return true;
	            } else {
	                return false;
	            }
	        }

	        return false;
	    }

	    public String getExtension(File f) {
	        String ext = null;
	        String s = f.getName();
	        int i = s.lastIndexOf('.');

	        if (i > 0 &&  i < s.length() - 1) {
	            ext = s.substring(i+1).toLowerCase();
	        }
	        return ext;
	    }

		public String getDescription() {
           return "Tea Source Files";
		}



	}




}
